@extends("layouts.backend.webadmin")
@section('page-header')
<h1>
    <small>knowledge index</small>
</h1>
@endsection

@section('content')
<link rel="stylesheet" href="{{env('APP_ROOT')}}/assets/select2/css/select2.min.css">
<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-xs-12">

            <div class="box">
                <div class="box-header">

                </div>
                <div class="box-body">
                    <table id="klg-list" class="table table-bordered table-striped">
                        <thead>
                        <tr>

                        </tr>
                        </thead>

                        <tbody>
                        </tbody>
                    </table>

                </div>
            </div>
        </div>
    </div>
    <input type="hidden" name="categories" value="{{$categoriesJson}}" >
</section>
@stop
@section('javascript-end')
<script src="{{env('APP_ROOT')}}/js/common.js"></script>
<script src="{{env('APP_ROOT')}}/assets/select2/js/select2.min.js"></script>
<script>
    $(function () {
        var m1 = manage();
        var tables = $('#klg-list').DataTable({
            //调整结构
            "dom": '<"dataTables_filter">rtip',
            "autoWidth":true,
            //国际化
            'oLanguage'   :{
                'sUrl'    : '{{env("APP_ROOT")}}/assets/bootstrap/js/zh_cn.json'
            },
            "initComplete": function( settings, json ) {
                @permission('knowledge-add')
                var button = '<a class="btn btn-primary projectmanage-add" href="{{route("knowledge.create")}}" role="button" style="float: left;"><i class="fa fa-plus-square" aria-hidden="true"></i> 提交</a>';
                @else
                var button = '';
                @endauth
                $('.dataTables_filter').html(button);
                var _$this = this;
                var categories = $.parseJSON($('input[name="categories"]').val());
                var _select = '<label><span>类别:</span><select class="form-control input-sm"><option value="0">--请选择--</option>';
                for (var i=0;i<categories.length;i++){
                    _select += '<option value="'+categories[i].id+'">'+categories[i].name+'</option>';
                }
                _select += '</select></label>';
                m1.filter(jQuery,_select,_$this,false);
            },
            "paging": true,
            "bSort": false,//禁止排序
            "processing": true,
            //开启服务器模式
            serverSide: true,
            ajax: {
                //指定数据源
                url: "{{route('knowledge.search')}}",
                type: "GET",
                data: function(d){
                    return userManage.getQueryCondition(d);
                },
                //接受服务器数据
                dataSrc:function(json){
                    getToken();
                    return json.data;
                }
            },
            pageLength: "{{  $pageNum }}",
            //data值对应数据库字段名
            columns: [
                {
                    'title':'编号',
                    "data": "klg_number"
                },
                {
                    'title':'标题',
                    "data": "title",
                    'width':'20%',
                    "render": function(data,type,row,meta){
                        return '<a href="{{route('knowledge.showpage')}}?id='+row['id']+'" target="_blank">'+ data +'</a>';
                    }
                },
                {
                    'title':'提交人',
                    'data':"user_name"
                },
                {
                    'title':'关联漏洞类型',
                    'data':'categories'
                },
                {
                    'title':'浏览量',
                    'data':'views'
                },
                /*{
                    'title':'发布时间',
                    'data':'created_at'
                },*/
                {
                    'title':'更新时间',
                    'data':'updated_at'
                },
                {
                    "title":"操作",
                    "targets": -2,
                    "data": null,
                    "render": function(data,type,row,meta){
                        return '<a href="{{env('APP_ROOT')}}/admin/knowledge/' + row['id'] + '" class="btn btn-primary knowledge-detail" type="button">详情</a>';
                    }
                }],
            //所有列过滤xss
            columnDefs: [{
                "targets": "_all",
                "render": $.fn.dataTable.render.text()
            }]
        });
        //自定义请求条件
        var userManage = {
            getQueryCondition : function (data){
                var param = {};
                var select = data.search.value.split('&');
                param.category_id = select[0]>0 ? select[0] : 0;
                param.keywords = select[1] ? select[1] : '';
                //组装分页参数
                param.start = data.start;
                param.length = data.length;
                param.draw = data.draw;
                return param;
            }
        }
    });
</script>
@endsection
